/**
 * Created by YUJO on 8/7/2016.
 */

$(document).ready(function(){
    //Radio style
    $('input').iCheck({
        checkboxClass: 'icheckbox_square-blue',
        radioClass: 'iradio_square-blue',
        increaseArea: '20%' // optional
    });

    $('.combobox').combobox();

    var agreement = [
        {
            "code":"ge001-001-001",
            "type":"general"
        },
        {
            "code":"ge001-001-002",
            "type":"general"
        },
        {
            "code":"dg002-003-001",
            "type":"dangerous"
        },
        {
            "code":"dg002-002-002",
            "type":"dangerous"
        },
        {
            "code":"ref001-001-001",
            "type":"reefer"
        }
    ]



    $("[data-toggle='popover']").popover({
            container:'body',
            placement:'top',
            trigger:'manual'
        }
    );

    //Spinner Style
    //$('.spinner').spinner();



    var agreement_no = getQueryStringByName("an");
    if(agreement_no!=''){
        $("#txtagrrmentno").val(agreement_no);
    }




    $('.oal-booking-preview-section').hide();


    $("#row_cargo_nature_detail").hide();
    $("#cargo_nature").change(function(){
        if($(this).val()=="GC"){
            $("#row_cargo_nature_detail").hide();
        }
        else{
            $("#row_cargo_nature_detail").show();
            $("#cargo_nature_detail").focus();
        }
    })


    $("#cargo_nature_detail").on("mouseover focus", function(){
        if($("#cargo_nature").val()=="RF"){
            $(this).attr('data-content','Please provide a detailed description of your reefer cargo (e.g. temperature, ventilation, further requirements, etc.).');
        }else if($("#cargo_nature").val()=="DG"){
            $(this).attr('data-content','Please attach your DG note here.');
        }
        else if($("#cargo_nature").val()=="AW"){
            $(this).attr('data-content','Please provide a detailed description of your awkward cargo (e.g. dimensions).');
        }
        $(this).popover('show');
    })

    $("#cargo_nature_detail").on("mouseout blur", function(){
        $(this).popover('hide');
    })

    $("#contract_commodity").on("mouseover focus", function(){
        $(this).popover('show');
    })

    $("#contract_commodity").on("mouseout blur", function(){
        $(this).popover('hide');
    })


    $(".error-field-icon").hide();
    $("#bk_commodity").val($("#contract_commodity").val());
    $("#contract_commodity").change(function() {
        $("#bk_commodity").val($(this).val()).removeClass("warning-field");
        $("#txtagrrmentno").removeClass("warning-field");
        $("#warning_booking_commodity").fadeOut("fast");
        $(this).parent().find(".error-field-icon").hide();
    })


    $("#bk_commodity").on("mouseover focus", function(){
        $(this).popover('show');
    })
    $("#bk_commodity").on("blur",function(){
        if($(this).val()!= $("#contract_commodity").val()){
            $(this).addClass("warning-field");
            $("#txtagrrmentno").addClass("warning-field");
            $("#warning_booking_commodity").fadeIn("fast");
            $(this).parent().find(".error-field-icon").show();
        }
        else{
            $(this).removeClass("warning-field");
            $("#txtagrrmentno").removeClass("warning-field");
            $("#warning_booking_commodity").fadeOut("fast");
            $(this).parent().find(".error-field-icon").hide();
        }
    })
    $("#warning_booking_commodity a").on("click",function(){
        $("#warning_booking_commodity").fadeOut();
    })




    $("#bk_commodity").on("mouseout blur", function(){
        $(this).popover('hide');
    })

    $("#book_ID").on("mouseover focus", function(){
        $(this).popover('show');
    })

    $("#book_ID").on("mouseout blur", function(){
        $(this).popover('hide');
    })


    $("#search_por").on("blur", function(){
        if($("#search_por").val()!=""){
            if($("#search_por").val()!="Liverpool (GB)" && $("#search_por").val()!= "Sowerby Bridge (GB)" ){
                $(this).popover('show');
            }
            else {
                $(this).popover('hide');
            }
        }
        else{
            $(this).popover('hide');
        }

    })


    $("#cb_container_gp20").on('ifChanged', function(event){
        if($(this).is(':checked')){
            $('#container_gp20').show();
        }
        else{
            $('#container_gp20').hide();
        }

    })

    $("#cb_container_gp40").on('ifChanged', function(event){
        if($(this).is(':checked')){
            $('#container_gp40').show();
        }
        else{
            $('#container_gp40').hide();
        }

    })

    $('#container_gp45').hide();
    $("#cb_container_gp45").on('ifChanged', function(event){
        if($(this).is(':checked')){
            $('#container_gp45').show();
        }
        else{
            $('#container_gp45').hide();
        }

    })
    $('#container_hq45').hide();
    $("#cb_container_hq45").on('ifChanged', function(event){
        if($(this).is(':checked')){
            $('#container_hq45').show();
        }
        else{
            $('#container_hq45').hide();
        }

    })


    $(".booking-form-container .truck-additional-info").hide();

    $(".booking-form-container .spinner input").spinner('changed', function(e, newVal, oldVal) {
        //alert(newVal);
    });

    $(".booking-form-vessel-information").click(function(){
        $(this).find("input:radio[name='rd_vessel']").iCheck('check');
        if($(this).hasClass("vessel-warning-bg")){

        }else if($(this).hasClass("vessel-ext-payment-bg")){

        }
    })

    $(".container-truck-info .date-time-box").focus(function(){
        $(this).parent().parent().parent().parent().find(".truck-additional-info").show();
    })

    $('#shipping_date').dateRangePicker({
        autoClose: true,
        singleDate : true,
        singleMonth: true,
        format: 'DD MMM YYYY',
        language: 'auto'
    });


    IniDateFormat();



    $(".oal-datepicker .input-group-addon").bind("click",function(evt){
        evt.stopPropagation();
        $(this).parent().find("input").focus().click();

    });
    var city = [
        {"name":"Sowerby Bridge (GB)"},
        {"name":"Liverpool (GB)"},
        {"name":"Hong kong (HK)"},
        {"name":"Shanghai (CN)"},
        {"name":"Shenzhen (CN)"}
    ]


    $(".error-message").hide();


    $("ul.weight-unit>li").click(function() {
        // do something…
        $(this).parent().parent().find("button").html("<span class=\"required\">*&nbsp;</span>" + $(this).text()+"&nbsp;<span class=\"caret\"></span>");
        var weightObj = $(this).parent().parent().parent().find(".oal-booking-container-number");
        var errorMsg =  $(this).parent().parent().find(".quote-error-message");
        var weight = $(this).parent().parent().parent().find(".oal-booking-container-number").val();

        if($(this).text().indexOf("KGs")>=0){
            if(weight> 25000 ){
                $(weightObj).focus();
                $(weightObj).addClass("error-field");
                $(errorMsg).show();
            }
            else{
                $(weightObj).removeClass("error-field");
                $(errorMsg).hide();
            }
        }
        else if($(this).text().indexOf("Pounds")>=0){
            if(weight> 55115 ){
                $(weightObj).focus();
                $(weightObj).addClass("error-field");
                $(errorMsg).show();
            }
            else{
                $(this).removeClass("error-field");
                $(errorMsg).hide();
            }
        }
        else if($(this).text().indexOf("Tons")>=0){
            if(weight> 25 ){
                $(weightObj).focus();
                $(weightObj).addClass("error-field");
                $(errorMsg).show();
            }
            else{
                $(weightObj).removeClass("error-field");
                $(errorMsg).hide();
            }
        }
    })

    $(".oal-booking-container-number").on('blur',function(e){
        if($(this).parent().find("button").html().indexOf("KGs")>=0){
            if($(this).val()> 25000 ){
                //$(this).focus();
                var that = this;
                setTimeout(function () {
                    $(that).focus();
                },100);
                $(this).addClass("error-field");
                $(this).parent().parent().find(".quote-error-message").show();
            }
            else{
                $(this).removeClass("error-field");
                $(this).parent().parent().find(".quote-error-message").hide();
            }
        }
        else if($(this).parent().find("button").html().indexOf("Pounds")>=0){
            if($(this).val()> 55115 ){
                //$(this).focus();
                var that = this;
                setTimeout(function () {
                    $(that).focus();
                },100);
                $(this).addClass("error-field");
                $(this).parent().parent().find(".quote-error-message").show();
            }
            else{
                $(this).removeClass("error-field");
                $(this).parent().parent().find(".quote-error-message").hide();
            }
        }
        else if($(this).parent().find("button").html().indexOf("Tons")>=0){
            if($(this).val()> 25 ){
                //$(this).focus();
                var that = this;
                setTimeout(function () {
                    $(that).focus();
                },100);
                $(this).addClass("error-field");
                $(this).parent().parent().find(".quote-error-message").show();

            }
            else{
                $(this).removeClass("error-field");
                $(this).parent().parent().find(".quote-error-message").hide();
            }
        }
    })



    $("#row_shipping_date").hide();
    $("input:radio[name='rdOBhaulage']").on('ifChecked', function(event){
        if($("input:radio[name='rdOBhaulage']:checked").val()=='CY'){
            $("#row_shipping_date").show();
            $(".container-truck-info").hide();
        }
        else{
            $("#row_shipping_date").hide();
            $(".container-truck-info").show();
        }
    })

    $("input:radio[name='rd_vessel']").on('ifChecked', function(event){
        if($("input:radio[name='rd_vessel']:checked").val()=='warning'){
                showmask();
                $(this).parent().parent().parent().parent().parent().find(".quote-message-box").show();
                // quote-message-box
        }
        else if($("input:radio[name='rd_vessel']:checked").val()=='valid'){
            $(".booking-form-container").find(".container-truck-info").find(".date-time-box").removeClass("warning-field");

        }
        else if($("input:radio[name='rd_vessel']:checked").val()=='payment'){
            showmask();
            $(".booking-form-container").find(".container-truck-info").find(".date-time-box").removeClass("warning-field");
            $(this).parent().parent().parent().parent().parent().find(".quote-message-box").show();
        }

    })

    $("#btn_confirm_time").click(function() {
        hidemask();
        if($("input:radio[name='rd_timeslot']:checked").val()=='ts2'){
            $(".booking-form-container").find(".container-truck-info:first").find(".date-time-box").addClass("warning-field");
        }
        else if($("input:radio[name='rd_timeslot']:checked").val()=='ts1'){
            $(".booking-form-container").find(".container-truck-info:first").find(".date-time-box").removeClass("warning-field");
        }
        $(this).parent().parent().hide();
    });

    $("#btn_accept_dem").click(function() {
        hidemask();
        $(this).parent().parent().hide();
    });
    $("#btn_deny_dem").click(function() {
        hidemask();
        $(this).parent().parent().hide();
        $(this).parent().parent().parent().parent().parent().parent().find("input:radio[name='rd_vessel']").iCheck('uncheck');
    });





    /*
    $(".booking-container-special-requirement").hide();
    $("input:checkbox").on('ifChanged', function(event){
        if($(this).is(':checked')){
            $(this).parent().parent().parent().parent().parent().find(".booking-container-special-requirement").show();
            $(this).parent().parent().parent().parent().parent().find(".booking-container-special-requirement").find('input').focus()
        }
        else{
            $(this).parent().parent().parent().parent().parent().find(".booking-container-special-requirement").hide();
        }
    })
    */


    IniContainerVessel();


    $("#search_vessel").click(function(){


        $('.booking-form-vessel-list').show();

        $('.booking-form-vessel').hide();
        $('.booking-form-vessel-list').append('<div class="loader">loading</div>');
        setTimeout(function(){
            $('.loader').hide();
            $('.booking-form-vessel').show();
        },500);

        var cont_height = $('.container-panel').height();
        var vessel_height = $('.vessel-panel').height();
        if(cont_height > vessel_height){
            $('.vessel-panel').height(cont_height);
        }
        else{
            $('.container-panel').height(vessel_height);
        }
    })


    $('.quote-vessel-validation').hide();

    $(".vessel-tabs-icon").on("mouseover", function(){
            if($(this).find(".icon-badge").hasClass("icon-badge-error")||$(this).find(".icon-badge").hasClass("icon-badge-warning")){
                $(this).find(".quote-vessel-validation").show();
            }
    })

    $(".vessel-tabs-icon").on("mouseout", function(){
        if($(this).find(".icon-badge").hasClass("icon-badge-error")||$(this).find(".icon-badge").hasClass("icon-badge-warning")){
            $(this).find(".quote-vessel-validation").hide();
        }
    })

    $(".additional-information").hide();
    $(".other-information-title").click(function(){
        $(".additional-information").toggle();
        $('.other-information-expand-collapse i').toggleClass("fa-angle-double-down");
        $('.other-information-expand-collapse i').toggleClass("fa-angle-double-up");
    })

    $(".more-container-type").hide();
    $("#lnk_more_conatiner_type").click(function(){
        $(".more-container-type").toggle();
        if($(this).html()=="<i class=\"fa fa-circle-plus\"></i> More"){
            $(this).text('Collapse');
        }
        else{
            $(this).html("<i class=\"fa fa-circle-plus\"></i> More");
        }


    })
    var agreement_no=["00001719-355",
        "00001719-358",
        "00001719-363",
        "00001719-367"
    ];

    $("#txtagrrmentno").typeahead({
        order: "asc",
        source: agreement_no
    });



    var od_data = ["Sowerby Bridge (GB)",
        "Liverpool (GB)",
        "Hong kong (HK)",
        "Shanghai (CN)",
        "Shenzhen (CN)"
    ];

    $("#search_por, #search_fnd").typeahead({
        order: "asc",
        source: od_data
    });



    $('.oal-schedule-list-item .btn-book-now').bind("click",function(){
        $('.oal-booking-section').hide();
        $('.oal-booking-preview-section').show();
    });


    $('#btnBacktoBooking').bind("click",function(){
        $('.oal-booking-section').show();
        $('.oal-booking-preview-section').hide();
        $('.booking-success').hide();
        $('.oal-booking-preview-section .oal-page-content-setting-action-bar').show();



    });
    $(".oal-preview-section").hide();
    $('#btnSearchBook').bind("click",function(){
        //$(".oal-booking-section").hide();
        // $(".oal-preview-section").show();

        swal({
            title: "Confirm to book?",
            type: "info",
            showCancelButton: true,
            confirmButtonColor: "#36b736",
            confirmButtonText: "Proceed to book",
            cancelButtonText: "Wait a minute",
            closeOnConfirm: false,
            closeOnCancel: false },
            function(isConfirm){
                if (isConfirm) {
                    swal({
                        title: "Thank you!",
                        text: "Your booking <span class=\"booking-number\">#12345678</span> is received. <br>Please check your e-mail <a href=\"mailto:mmikki.yuen@oocl.com\">mmikki.yuen@oocl.com</a>",
                        html: true,
                        type:"success"
                    });
                }
                else {
                    swal({
                        title:"Nothing changed.",
                        type:"warning",
                        timer: 2000
                    });
                }
            })

    });

    function jumpUrl(count) {
        window.setTimeout(function(){
            count--;
            if(count > 0) {
                $(".second-count").text(count);
                jumpUrl(count);
            } else {
                location.href= 'homepage.html';
            }

        }, 1000);
    }

});

function IniDateFormat(){
    $('.container-truck-info .date-time-box').each(function(){
        $(this).dateRangePicker({
            autoClose: true,
            singleDate : true,
            singleMonth: true,
            format: 'DD MMM YYYY hh:mm A',
            language: 'auto',
            time: {
                enabled: true
            }
        });
    });
}

function IniContainerVessel(){
    //show container and vessel list by default
    var cont_height = $('.container-panel').height();
    //var vessel_height = $('.vessel-panel').height();
    $('.container-panel').height(cont_height);
    $('.vessel-panel').height(cont_height);


}

function getQueryStringByName(name) {
    var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}

function showmask(){
    //scrollTop: $(obj).offset().top + "px";
    //$(document.body).css({"overflow":"hidden"});
    $(".page-mask").height($(document.body).height());
    $(".page-mask").width($(document.body).width());
    $(".page-mask").show();
}
function hidemask(){
    //$(document.body).css({"overflow":"auto"});
    $(".page-mask").hide();
}







